package Main;


import java.awt.Color;

public class Histogram {
	private int[] sum = new int[256];
	private int[] totalSum = new int[256];

	public Histogram(Color[][] c) {
		for (int i = 0; i < 256; i++) {
			sum[i] = 0;
		}
		for (int x = 0; x < c[0].length; x++)
			for (int y = 0; y < c.length; y++) {
				int i = c[y][x].getRed();
				sum[i]++;
			}
		totalSum[0] = sum[0];
		for (int i = 1; i < 256; i++)
			totalSum[i] = totalSum[i - 1] + sum[i];
	}

	/**
	 * Finds the largest number in the sum[] array for later use with scale.
	 * 
	 * @return returns the max number.
	 */
	public int max() {
		int max = 0;
		for (int i = 0; i < sum.length; i++)
			if (sum[i] > max)
				max = sum[i];
		return max;
	}

	/**
	 * Creates a black line for a base line for the histogram and sets the
	 * background to white.
	 * 
	 * @param c
	 *            is the 512x512 image you wish to draw it on.
	 * @return returns the new 512x512 with white backgrounds and black line.
	 */
	public Color[][] blackLine(Color[][] c) {
		for (int x = 0; x < c[0].length; x++)
			for (int y = 0; y < c.length; y++)
				c[y][x] = Color.white;
		int y = 412;
		for (int x = 128; x <= 384; x++) {
			c[y][x] = Color.black;
		}
		return c;
	}

	public Color[][] redLines(Color[][] c) {
		int x = 128;
		int max = max();
		System.out.println(max);
		double scale = 400.0 / max;
		System.out.println(scale);
		double picScale = 400.0 / totalSum[255];
		for (int i = 0; i < sum.length; i++) {
			x++;
			for (int y = 412; y > 412 - (sum[i] * scale); y--)
				c[y][x] = Color.red;
			int yC = (int) (412 - totalSum[i] * picScale);
			c[yC][x] = Color.black;
		}
		return c;
	}
}